<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item>
          <el-input v-model="filters.label" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <kt-button :label="$t('action.search')" perms="sys:cephfs:view" type="primary" @click="findPage(null)"/>
        </el-form-item>
        <el-form-item>
          <kt-button :label="$t('action.add')" perms="sys:cephfs:add" type="primary" @click="handleAdd"/>
        </el-form-item>
      </el-form>
    </div>
    <!--表格内容栏-->
    <kt-table permsEdit="sys:cephfs:edit" permsDelete="sys:cephfs:delete" permsDashboard="sys:cephfs:dashboard"
              :data="pageResult" :columns="columns"
              @findPage="findPage" @handleDashboard="handleDashboard" @handleEdit="handleEdit"
              @handleDelete="handleDelete">
    </kt-table>
    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
        <!--<el-form-item label="编号" prop="id">-->
        <!--<el-input v-model="dataForm.id" auto-complete="off"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="总容量" prop="capacity">
          <el-input v-model="dataForm.capacity" auto-complete="off"></el-input>
        </el-form-item>
        <!--<el-form-item label="已用容量" prop="used">-->
        <!--<el-input v-model="dataForm.used" auto-complete="off"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="可用容量" prop="avail">-->
        <!--<el-input v-model="dataForm.avail" auto-complete="off"></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="监视器" prop="monitors" v-if="operation">
          <el-input v-model="dataForm.monitors" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="user" v-if="operation">
          <el-input v-model="dataForm.user" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密钥" prop="keyBase64" v-if="operation">
          <el-input v-model="dataForm.keyBase64" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="挂载路径" prop="mountPath" v-if="operation">
          <el-input v-model="dataForm.mountPath" auto-complete="off"></el-input>
        </el-form-item>
        <!--<el-form-item label="k8s密钥" prop="k8sSecret">-->
        <!--<el-input v-model="dataForm.k8sSecret" auto-complete="off"></el-input>-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import KtTable from "@/views/Core/CephfsTable"
  import KtButton from "@/views/Core/KtButton"
  import {format} from "@/utils/datetime"

  export default {
    components: {
      KtTable,
      KtButton
    },
    data() {
      return {
        size: 'small',
        filters: {
          label: ''
        },
        columns: [
          {prop: "id", label: "编号", minWidth: 100},
          {prop: "capacity", label: "总容量", minWidth: 100},
          {prop: "used", label: "已用容量", minWidth: 100},
          {prop: "avail", label: "可用容量", minWidth: 100},
          {prop: "monitors", label: "监视器", minWidth: 100},
          {prop: "user", label: "用户名", minWidth: 100},
          {prop: "keyBase64", label: "用户密钥", minWidth: 100},
          {prop: "mountPath", label: "挂载路径", minWidth: 100},
          // {prop:"k8sSecret", label:"k8s密钥", minWidth:100},
        ],
        pageRequest: {pageNum: 1, pageSize: 8},
        pageResult: {},

        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          label: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ]
        },
        // 新增编辑界面数据
        dataForm: {
          id: null,
          capacity: null,
          used: null,
          avail: null,
          monitors: null,
          user: null,
          keyBase64: null,
          mountPath: null,
          k8sSecret: null,
        }
      }
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        if (data !== null) {
          this.pageRequest = data.pageRequest
        }
        this.pageRequest.columnFilters = {label: {name: 'label', value: this.filters.label}}
        this.$api.cephfs.findPage(this.pageRequest).then((res) => {
          this.pageResult = res.data
        }).then(data != null ? data.callback : '')
      },
      // 批量删除
      handleDelete: function (data) {
        this.$api.cephfs.batchDelete(data.params).then(data != null ? data.callback : '')
      },
      // 显示新增界面
      handleAdd: function () {
        this.editDialogVisible = true
        this.operation = true
        this.dataForm = {
          id: null,
          capacity: 10000,
          used: null,
          avail: null,
          monitors: "node1:6789,node2:6789,node3:6789",
          user: "admin",
          keyBase64: "AQAK0GhchiCgIRAAdiRHPj7StHcsHPUCIysv9A==",
          mountPath: "/mnt/cecii/cephfs_0001",
          k8sSecret: null,
        }
      },
      handleDashboard: function (params) {
        // open("http://192.168.1.43:8443");
        // open("http://localhost:8090/#/8443");
        // alert(JSON.stringify(params.row));
        // alert(params.row.monitors);
        var url = params.row.monitors.split(":")[0];
        if (!url) {
          alert("error");
        } else {
          url = "http://" + url + ":8443";
          open(url);
        }

      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.editDialogVisible = true
        this.operation = false
        this.dataForm = Object.assign({}, params.row)
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true
              let params = Object.assign({}, this.dataForm)
              this.$api.cephfs.save(params).then((res) => {
                if (res.code == 200) {
                  this.$message({message: '操作成功', type: 'success'})
                } else {
                  this.$message({message: '操作失败, ' + res.msg, type: 'error'})
                }
                this.editLoading = false
                this.$refs['dataForm'].resetFields()
                this.editDialogVisible = false
                this.findPage(null)
              })
            })
          }
        })
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property])
      }
    },
    mounted() {
    }
  }
</script>

<style scoped>

</style>
